home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / g_quake / advqcc.zip / CMDLIB.C < prev    next >
C/C++ Source or Header  |  1996-09-25  |  8KB  |  575 lines

  1. // cmdlib.c
  2.  
  3. #include "cmdlib.h"
  4. #include <time.h>
  5.  
  6. #define PATHSEPERATOR   '/'
  7.  
  8. // set these before calling CheckParm
  9. int myargc;
  10. char **myargv;
  11.  
  12. char    com_token[1024];
  13. int        com_eof;
  14.  
  15. /*
  16. ==============
  17. COM_Parse
  18.  
  19. Parse a token out of a string
  20. ==============
  21. */
  22. char *COM_Parse (char *data)
  23. {
  24.     int        c;
  25.     int        len;
  26.     
  27.     len = 0;
  28.     com_token[0] = 0;
  29.     
  30.     if (!data)
  31.         return NULL;
  32.         
  33. // skip whitespace
  34. skipwhite:
  35.     while ( (c = *data) <= ' ')
  36.     {
  37.         if (c == 0)
  38.         {
  39.             com_eof = true;
  40.             return NULL;            // end of file;
  41.         }
  42.         data++;
  43.     }
  44.     
  45. // skip // comments
  46.     if (c=='/' && data[1] == '/')
  47.     {
  48.         while (*data && *data != '\n')
  49.             data++;
  50.         goto skipwhite;
  51.     }
  52.     
  53.  
  54. // handle quoted strings specially
  55.     if (c == '\"')
  56.     {
  57.         data++;
  58.         do
  59.         {
  60.             c = *data++;
  61.             if (c=='\"')
  62.             {
  63.                 com_token[len] = 0;
  64.                 return data;
  65.             }
  66.             com_token[len] = c;
  67.             len++;
  68.         } while (1);
  69.     }
  70.  
  71. // parse single characters
  72.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  73.     {
  74.         com_token[len] = c;
  75.         len++;
  76.         com_token[len] = 0;
  77.         return data+1;
  78.     }
  79.  
  80. // parse a regular word
  81.     do
  82.     {
  83.         com_token[len] = c;
  84.         data++;
  85.         len++;
  86.         c = *data;
  87.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  88.             break;
  89.     } while (c>32);
  90.     
  91.     com_token[len] = 0;
  92.     return data;
  93. }
  94.  
  95.  
  96. #if 0
  97.  
  98. /*
  99. ================
  100. filelength
  101. ================
  102. */
  103. int filelength (int handle)
  104. {
  105.     struct stat    fileinfo;
  106.     
  107.     if (fstat (handle,&fileinfo) == -1)
  108.     {
  109.         Error ("Error fstating");
  110.     }
  111.  
  112.     return fileinfo.st_size;
  113. }
  114.  
  115. int tell (int handle)
  116. {
  117.     return lseek (handle, 0, SEEK_CUR);
  118. }
  119. #endif
  120.  
  121.  
  122. char *strupr (char *start)
  123. {
  124.     char    *in;
  125.     in = start;
  126.     while (*in)
  127.     {
  128.         *in = toupper(*in);
  129.         in++;
  130.     }
  131.     return start;
  132. }
  133.  
  134. char *strlower (char *start)
  135. {
  136.     char    *in;
  137.     in = start;
  138.     while (*in)
  139.     {
  140.         *in = tolower(*in);
  141.         in++;
  142.     }
  143.     return start;
  144. }
  145.  
  146.  
  147. /*
  148. =============================================================================
  149.  
  150.                         MISC FUNCTIONS
  151.  
  152. =============================================================================
  153. */
  154.  
  155. /*
  156. =================
  157. Error
  158.  
  159. For abnormal program terminations
  160. =================
  161. */
  162. void Error (char *error, ...)
  163. {
  164.     va_list argptr;
  165.  
  166.     printf ("\n************ ERROR ************\n");
  167.  
  168.     va_start (argptr,error);
  169.     vprintf (error,argptr);
  170.     va_end (argptr);
  171.     printf ("\n");
  172.     exit (1);
  173. }
  174.  
  175.  
  176. /*
  177. =================
  178. CheckParm
  179.  
  180. Checks for the given parameter in the program's command line arguments
  181. Returns the argument number (1 to argc-1) or 0 if not present
  182. =================
  183. */
  184. int CheckParm (char *check)
  185. {
  186.     int             i;
  187.  
  188.     for (i = 1;i<myargc;i++)
  189.     {
  190.         if ( !stricmp(check, myargv[i]) )
  191.             return i;
  192.     }
  193.  
  194.     return 0;
  195. }
  196.  
  197.  
  198. #ifndef O_BINARY
  199. #define O_BINARY 0
  200. #endif
  201.  
  202. int SafeOpenWrite (char *filename)
  203. {
  204.     int     handle;
  205.  
  206.     umask (0);
  207.     
  208.     handle = open(filename,O_WRONLY | O_CREAT | O_TRUNC | O_BINARY
  209.     , 0666);
  210.  
  211.     if (handle == -1)
  212.         Error ("Error opening %s: %s",filename,strerror(errno));
  213.  
  214.     return handle;
  215. }
  216.  
  217. int SafeOpenRead (char *filename)
  218. {
  219.     int     handle;
  220.  
  221.     handle = open(filename,O_RDONLY | O_BINARY);
  222.  
  223.     if (handle == -1)
  224.         Error ("Error opening %s: %s",filename,strerror(errno));
  225.  
  226.     return handle;
  227. }
  228.  
  229.  
  230. void SafeRead (int handle, void *buffer, long count)
  231. {
  232.     if (read (handle,buffer,count) != count)
  233.         Error ("File read failure");
  234. }
  235.  
  236.  
  237. void SafeWrite (int handle, void *buffer, long count)
  238. {
  239.     if (write (handle,buffer,count) != count)
  240.         Error ("File write failure");
  241. }
  242.  
  243.  
  244. void *SafeMalloc (long size)
  245. {
  246.     void *ptr;
  247.  
  248.     ptr = malloc (size);
  249.  
  250.     if (!ptr)
  251.         Error ("Malloc failure for %lu bytes",size);
  252.  
  253.     return ptr;
  254. }
  255.  
  256.  
  257. /*
  258. ==============
  259. LoadFile
  260. ==============
  261. */
  262. long    LoadFile (char *filename, void **bufferptr)
  263. {
  264.     int             handle;
  265.     long    length;
  266.     void    *buffer;
  267.  
  268.     handle = SafeOpenRead (filename);
  269.     length = filelength (handle);
  270.     buffer = SafeMalloc (length+1);
  271.     ((byte *)buffer)[length] = 0;
  272.     SafeRead (handle, buffer, length);
  273.     close (handle);
  274.  
  275.     *bufferptr = buffer;
  276.     return length;
  277. }
  278.  
  279.  
  280. /*
  281. ==============
  282. SaveFile
  283. ==============
  284. */
  285. void    SaveFile (char *filename, void *buffer, long count)
  286. {
  287.     int             handle;
  288.  
  289.     handle = SafeOpenWrite (filename);
  290.     SafeWrite (handle, buffer, count);
  291.     close (handle);
  292. }
  293.  
  294.  
  295.  
  296. void DefaultExtension (char *path, char *extension)
  297. {
  298.     char    *src;
  299. //
  300. // if path doesn't have a .EXT, append extension
  301. // (extension should include the .)
  302. //
  303.     src = path + strlen(path) - 1;
  304.  
  305.     while (*src != PATHSEPERATOR && src != path)
  306.     {
  307.         if (*src == '.')
  308.             return;                 // it has an extension
  309.         src--;
  310.     }
  311.  
  312.     strcat (path, extension);
  313. }
  314.  
  315.  
  316. void DefaultPath (char *path, char *basepath)
  317. {
  318.     char    temp[128];
  319.  
  320.     if (path[0] == PATHSEPERATOR)
  321.         return;                   // absolute path location
  322.     strcpy (temp,path);
  323.     strcpy (path,basepath);
  324.     strcat (path,temp);
  325. }
  326.  
  327.  
  328. void    StripFilename (char *path)
  329. {
  330.     int             length;
  331.  
  332.     length = strlen(path)-1;
  333.     while (length > 0 && path[length] != PATHSEPERATOR)
  334.         length--;
  335.     path[length] = 0;
  336. }
  337.  
  338. void    StripExtension (char *path)
  339. {
  340.     int             length;
  341.  
  342.     length = strlen(path)-1;
  343.     while (length > 0 && path[length] != '.')
  344.     {
  345.         length--;
  346.         if (path[length] == '/')
  347.             return;        // no extension
  348.     }
  349.     if (length)
  350.         path[length] = 0;
  351. }
  352.  
  353.  
  354. /*
  355. ====================
  356. Extract file parts
  357. ====================
  358. */
  359. void ExtractFilePath (char *path, char *dest)
  360. {
  361.     char    *src;
  362.  
  363.     src = path + strlen(path) - 1;
  364.  
  365. //
  366. // back up until a \ or the start
  367. //
  368.     while (src != path && *(src-1) != PATHSEPERATOR)
  369.         src--;
  370.  
  371.     memcpy (dest, path, src-path);
  372.     dest[src-path] = 0;
  373. }
  374.  
  375. void ExtractFileBase (char *path, char *dest)
  376. {
  377.     char    *src;
  378.  
  379.     src = path + strlen(path) - 1;
  380.  
  381. //
  382. // back up until a \ or the start
  383. //
  384.     while (src != path && *(src-1) != PATHSEPERATOR)
  385.         src--;
  386.  
  387.     while (*src && *src != '.')
  388.     {
  389.         *dest++ = *src++;
  390.     }
  391.     *dest = 0;
  392. }
  393.  
  394. void ExtractFileExtension (char *path, char *dest)
  395. {
  396.     char    *src;
  397.  
  398.     src = path + strlen(path) - 1;
  399.  
  400. //
  401. // back up until a . or the start
  402. //
  403.     while (src != path && *(src-1) != '.')
  404.         src--;
  405.     if (src == path)
  406.     {
  407.         *dest = 0;    // no extension
  408.         return;
  409.     }
  410.  
  411.     strcpy (dest,src);
  412. }
  413.  
  414.  
  415. /*
  416. ==============
  417. ParseNum / ParseHex
  418. ==============
  419. */
  420. long ParseHex (char *hex)
  421. {
  422.     char    *str;
  423.     long    num;
  424.  
  425.     num = 0;
  426.     str = hex;
  427.  
  428.     while (*str)
  429.     {
  430.         num <<= 4;
  431.         if (*str >= '0' && *str <= '9')
  432.             num += *str-'0';
  433.         else if (*str >= 'a' && *str <= 'f')
  434.             num += 10 + *str-'a';
  435.         else if (*str >= 'A' && *str <= 'F')
  436.             num += 10 + *str-'A';
  437.         else
  438.             Error ("Bad hex number: %s",hex);
  439.         str++;
  440.     }
  441.  
  442.     return num;
  443. }
  444.  
  445.  
  446. long ParseNum (char *str)
  447. {
  448.     if (str[0] == '$')
  449.         return ParseHex (str+1);
  450.     if (str[0] == '0' && str[1] == 'x')
  451.         return ParseHex (str+2);
  452.     return atol (str);
  453. }
  454.  
  455.  
  456.  
  457. /*
  458. ============================================================================
  459.  
  460.                     BYTE ORDER FUNCTIONS
  461.  
  462. ============================================================================
  463. */
  464.  
  465. #ifdef __BIG_ENDIAN__
  466.  
  467. short   LittleShort (short l)
  468. {
  469.     byte    b1,b2;
  470.  
  471.     b1 = l&255;
  472.     b2 = (l>>8)&255;
  473.  
  474.     return (b1<<8) + b2;
  475. }
  476.  
  477. short   BigShort (short l)
  478. {
  479.     return l;
  480. }
  481.  
  482. long    LittleLong (long l)
  483. {
  484.     byte    b1,b2,b3,b4;
  485.  
  486.     b1 = l&255;
  487.     b2 = (l>>8)&255;
  488.     b3 = (l>>16)&255;
  489.     b4 = (l>>24)&255;
  490.  
  491.     return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
  492. }
  493.  
  494. long    BigLong (long l)
  495. {
  496.     return l;
  497. }
  498.  
  499.  
  500. float    LittleFloat (float l)
  501. {
  502.     union {byte b[4]; float f;} in, out;
  503.     
  504.     in.f = l;
  505.     out.b[0] = in.b[3];
  506.     out.b[1] = in.b[2];
  507.     out.b[2] = in.b[1];
  508.     out.b[3] = in.b[0];
  509.     
  510.     return out.f;
  511. }
  512.  
  513. float    BigFloat (float l)
  514. {
  515.     return l;
  516. }
  517.  
  518.  
  519. #else
  520.  
  521.  
  522. short   BigShort (short l)
  523. {
  524.     byte    b1,b2;
  525.  
  526.     b1 = l&255;
  527.     b2 = (l>>8)&255;
  528.  
  529.     return (b1<<8) + b2;
  530. }
  531.  
  532. short   LittleShort (short l)
  533. {
  534.     return l;
  535. }
  536.  
  537.  
  538. long    BigLong (long l)
  539. {
  540.     byte    b1,b2,b3,b4;
  541.  
  542.     b1 = l&255;
  543.     b2 = (l>>8)&255;
  544.     b3 = (l>>16)&255;
  545.     b4 = (l>>24)&255;
  546.  
  547.     return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
  548. }
  549.  
  550. long    LittleLong (long l)
  551. {
  552.     return l;
  553. }
  554.  
  555. float    BigFloat (float l)
  556. {
  557.     union {byte b[4]; float f;} in, out;
  558.     
  559.     in.f = l;
  560.     out.b[0] = in.b[3];
  561.     out.b[1] = in.b[2];
  562.     out.b[2] = in.b[1];
  563.     out.b[3] = in.b[0];
  564.     
  565.     return out.f;
  566. }
  567.  
  568. float    LittleFloat (float l)
  569. {
  570.     return l;
  571. }
  572.  
  573. #endif
  574.  
  575.